package com.crm.system.controller.productManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.productManager.CustomerMemberProduct;
import com.crm.model.qo.productManager.CustomerMemberProductAddQO;
import com.crm.model.vo.productManager.ContractMemberProductVO;
import com.crm.model.vo.productManager.CustomerMemberProductVO;
import com.crm.system.service.productManager.CustomerMemberProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yincuimei
 * @Date 2021/4/12 14:46
 * 产品管理--会员产品
 */
@Slf4j
@RestController
@RequestMapping("/customerMemberProduct")
public class CustomerMemberProductController {
    @Autowired
    private CustomerMemberProductService memberProductService;

    /**
     * 产品管理--会员产品列表
     * @return
     * @author yincuimei
     */
    @PostMapping("/selectCustomerMemberProductList")
    public DataResponse selectCustomerMemberProductVOList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                          @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                          @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                          @RequestParam(value="sysType",required = false) String sysType,//系统类型(用户-user/管理-management)
                                                          @RequestParam(value = "productStatus", required = false) String productStatus,//查询条件
                                                          @RequestParam(value = "memberCardName", required = false) String memberCardName //会员卡名称
    ){
        DhPageInfo<CustomerMemberProductVO> customerProductVODhPageInfo = memberProductService.selectCustomerMemberProductVOList(productStatus,pageIndex,pageSize,sortName,sortOrder,memberCardName,sysType);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVODhPageInfo);
    }

    /**
     * 产品管理--新增会员产品
     * @param memberProductAddQO ---会员产品信息
     * @return
     * @author yincuimei
     */
    @PostMapping("/addCustomerMemberProduct")
    public DataResponse addCustomerMemberProduct(@RequestBody CustomerMemberProductAddQO memberProductAddQO, @RequestParam(value="file",required = false) MultipartFile file){
        try{
            JSONObject jsonObject = memberProductService.insertCustomerMemberProduct(memberProductAddQO,file);
            if(jsonObject.containsKey("addCustomerMemberProduct")){//添加会员产品成功
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            if(jsonObject.containsKey("memberCardNameRepeat")){//会员卡名称重复
                return new DhResponse(ResponseCodeEnum.PRODUCT_MEMBER_CARD_NAME_REPEAT.getResponseCode());
            }
            if(jsonObject.containsKey("memberProductCodeRepeat")){//会员产品编码重复
                return new DhResponse(ResponseCodeEnum.PRODUCT_MEMBER_PRODUCT_CODE_REPEAT.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }catch (Exception e){
            log.error("CustomerMemberProductController addCustomerMemberProduct error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 会员产品列表--停用、启用操作
     * @param memberProductId ---主键
     * @param productStatus   ---产品状态
     * @return
     * @author yincuimei
     */
    @PostMapping("/updateProductStatus")
    public DataResponse updateProductStatus(@RequestParam(value="memberProductId",required = true) String memberProductId,
                                        @RequestParam(value="productStatus",required = true) String productStatus
    ) {
        Boolean result = memberProductService.updateProductStatusById(memberProductId, productStatus);
        if (result) {
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }
        return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /***
     * @Description: 添加合同查询合同的会员产品
     * @Param: pageIndex 页码
     * @Param: pageSize 每页显示条数
     * @Param: productName 产品名称
     * @Author: luojie
     * @Date: 2021/4/19 14:12
     */
    @PostMapping("/selectContractMemberProductList")
    public DataResponse selectContractMemberProductList(@RequestBody JSONObject body
    ){
        Integer pageIndex = body.getInteger("pageIndex");
        Integer pageSize = body.getInteger("pageSize");
        String productName = body.getString("productName");
        String whetherRenew = body.getString("whetherRenew");
        DhPageInfo<ContractMemberProductVO> ContractMemberProductVO = memberProductService.selectContractMemberProductList(pageIndex,pageSize,productName,whetherRenew);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),ContractMemberProductVO);
    }

    /**
     * 新增商机--会员产品列表
     * @return
     * @author baijian
     */
    @PostMapping("/selectSjCustomerMemberProductVOList")
    public DataResponse selectSjCustomerMemberProductVOList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                          @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                          @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                          @RequestParam(value = "productStatus", required = false) String productStatus,//查询条件
                                                          @RequestParam(value = "memberCardName", required = false) String memberCardName, //会员卡名称
                                                          @RequestParam(value = "tag",required = false) String tag  //1会员
    ){
        DhPageInfo<CustomerMemberProductVO> customerProductVODhPageInfo = memberProductService.selectSjCustomerMemberProductVOList(productStatus,pageIndex,pageSize,sortName,sortOrder,memberCardName,tag);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVODhPageInfo);
    }

    /**
     * 查询所有【启用状态】的会员卡名称
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectMemberCardNameByProductStatus")
    public DataResponse selectMemberCardNameByProductStatus(){
        List<String> memberProductNameList = memberProductService.selectMemberCardNameByProductStatus();
        if(memberProductNameList != null && memberProductNameList.size() > 0){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),memberProductNameList);
        }
        return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }
}
